package com.delong.spring.handler.market;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.delong.cache.AppCache;
import com.delong.model.market.DataSource;
import com.delong.service.market.DataSourceService;
import com.delong.service.market.ScheduleContentService;

@Controller
@RequestMapping("/datasource")
public class DataSourceController {
	private DataSourceService service = new DataSourceService();

	@RequestMapping("/list")
	public String getappList(Map<String, Object> model, ServletRequest req,
			@RequestParam(value = "source_id", defaultValue = "0") int sourceId) {
		HttpServletRequest request = (HttpServletRequest) req;
		model.put("source_id", sourceId);
		model.put("username", request.getSession().getAttribute("username"));
		return "market/DataSource";
	}

	@RequestMapping(value = "/getElement", method = RequestMethod.GET)
	@ResponseBody
	public String getElement(@RequestParam(value = "id", defaultValue = "0") int id) {
		JSONObject result = new JSONObject();

		DataSource element = this.service.getElementById(id);
		result.put("data", element);
		result.put("code", 0);
		return result.toJSONString();
	}

	@RequestMapping(value = "/getdata", method = RequestMethod.POST)
	@ResponseBody
	public String getPageListData(Map<String, Object> model,
			@RequestParam(value = "sourceId", defaultValue = "0") int sourceId,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "rows", defaultValue = "15") int rows,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "packagename", defaultValue = "") String packagename) {

		page = page - 1;
		if (title.trim() != "") {
			title = "%" + title + "%";
		}
		if (packagename.trim() != "") {
			packagename = "%" + packagename + "%";
		}
		List<DataSource> elements = this.service.getListElement(sourceId, page * rows, rows, title, packagename);
		int total = this.service.getListElementCount(sourceId, title, packagename);
		JSONObject result = new JSONObject();
		result.put("rows", elements);
		result.put("total", total);
		return result.toJSONString();
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String appAdd(Map<String, Object> model, @RequestParam(value = "sourceId", defaultValue = "0") int sourceId,
			@RequestParam(value = "appId", defaultValue = "0") int appId,
			@RequestParam(value = "id", defaultValue = "0") int id,
			@RequestParam(value = "order_priority", defaultValue = "0") int order_priority

	) {
		DataSource element = new DataSource();
		element.setSource_uuid(sourceId);
		element.setApp_id(appId);
		element.setOrder_priority(order_priority);
		if (id == 0) {
			service.addElement(element);
		} else {
			element.setId(id);
			service.changeElement(element);
		}
		ScheduleContentService baseService = new ScheduleContentService();
		List<Integer> scheduleIds = baseService.selectElementByContent(String.valueOf(sourceId));
		AppCache cache = new AppCache();
		cache.deletePagesByScheduleId(scheduleIds);
		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("message", "success");
		return result.toJSONString();
	}

	@RequestMapping("/delete")
	@ResponseBody
	public String deleteElement(Map<String, Object> model,

	@RequestParam(value = "id", defaultValue = "0") int id,
			@RequestParam(value = "sourceId", defaultValue = "0") int sourceId) {
		ScheduleContentService baseService = new ScheduleContentService();
		List<Integer> scheduleIds = baseService.selectElementByContent(String.valueOf(sourceId));
		AppCache cache = new AppCache();
		cache.deletePagesByScheduleId(scheduleIds);
		this.service.deleteElements(id);

		JSONObject result = new JSONObject();
		result.put("code", 0);
		return result.toJSONString();
	}
}
